package article

import (
	"admin.supervise.limaochuxing.top/admin/schemas/req"
	"admin.supervise.limaochuxing.top/admin/service/article"
	"admin.supervise.limaochuxing.top/core"
	"admin.supervise.limaochuxing.top/core/request"
	"admin.supervise.limaochuxing.top/core/response"
	"admin.supervise.limaochuxing.top/middleware"
	"admin.supervise.limaochuxing.top/util"

	"github.com/gin-gonic/gin"
)

var ArticleGroup = core.Group("/", newArticleHandler, regArticle, middleware.TokenAuth())

func newArticleHandler(srv article.IArticleService) *articleHandler {
	return &articleHandler{srv: srv}
}

func regArticle(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *articleHandler) {
		rg.GET("/article/list", handle.list)
		rg.GET("/article/detail", handle.detail)
		rg.POST("/article/add", handle.add)
		rg.POST("/article/edit", handle.edit)
		rg.POST("/article/del", handle.del)
		rg.POST("/article/change", handle.change) // 添加新的路由
	})
}



type articleHandler struct {
	srv article.IArticleService
}

//change 修改文章状态
func (hd articleHandler) change(c *gin.Context) {
	var changeReq req.ArticleChangeReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &changeReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Change(changeReq.Id))
}

//list article列表
func (hd articleHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.ArticleListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

//detail article详情
func (hd articleHandler) detail(c *gin.Context) {
	var detailReq req.ArticleDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

//add article新增
func (hd articleHandler) add(c *gin.Context) {
	var addReq req.ArticleAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(addReq))
}

//edit article编辑
func (hd articleHandler) edit(c *gin.Context) {
	var editReq req.ArticleEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

//del article删除
func (hd articleHandler) del(c *gin.Context) {
	var delReq req.ArticleDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.Id))
}